package br.com.fatecpg.mamaeconomiza.library;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

public class ProductFunctions {

	private JSONParser jsonParser;

	private static String productURL = "http://mamaeconomiza.br22.com/android_access/products/index_products.php";
	//private static String productURL = "http://10.0.2.2:8080/android_access/products/index_products.php";

	private static String create_tag = "create_product";
	private static String all_products_tag = "all_products";
	private static String get_product_tag = "get_product";
	private static String search_product_tag = "search_product";

	/*
	 * private static String register_tag = "register"; private static String
	 * delete_tag = "delete"; private static String update_tag = "update";
	 */

	// constructor
	public ProductFunctions() {
		jsonParser = new JSONParser();
	}

	/**
	 * function create product Request
	 * 
	 * @param name
	 * @param maker
	 * @param price
	 * @param description
	 * @param shop
	 * @param address
	 * */
	public JSONObject createProduct(String name, String maker, String price,
			String description, String shop, String address) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", create_tag));
		params.add(new BasicNameValuePair("name", name));
		params.add(new BasicNameValuePair("maker", maker));
		params.add(new BasicNameValuePair("price", price));
		params.add(new BasicNameValuePair("description", description));
		params.add(new BasicNameValuePair("shop", shop));
		params.add(new BasicNameValuePair("address", address));
		JSONObject json = jsonParser.getJSONFromUrl(productURL, params);
		// return json
		// Log.e("JSON", json.toString());
		return json;
	}

	/**
	 * function all product Request
	 * */
	public JSONObject allProduct() {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", all_products_tag));		
		JSONObject json = jsonParser.getJSONFromUrl(productURL, params);
		// return json
		// Log.e("JSON", json.toString());
		return json;
	}

	/**
	 * function one product
	 * @param pid
	 *
	 * */
	public JSONObject getProduct(String pid) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", get_product_tag));
		params.add(new BasicNameValuePair("pid", pid));
		JSONObject json = jsonParser.getJSONFromUrl(productURL, params);
		// return json
		// Log.e("JSON", json.toString());
		return json;
	}
	
	/**
	 * function search product Request
	 * @param search
	 * */
	public JSONObject searchProduct(String search) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", search_product_tag));
		params.add(new BasicNameValuePair("search", search));
		JSONObject json = jsonParser.getJSONFromUrl(productURL, params);
		// return json
		// Log.e("JSON", json.toString());
		return json;
	}

}
